//
//  CoserViewController.m
//  OtakuWorld
//
//  Created by iOS Two on 14-9-25.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "CoserViewController.h"
#import "UIImageView+MJWebCache.h"
#import "PicViewController.h"

@interface CoserViewController ()

@end

@implementation CoserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    if (version >= 7) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"qwe_02"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"qwe_02"] forBarMetrics:UIBarMetricsDefault];
    }
    [MobClick beginLogPageView:@"CoserViewController"];

}

-(void)viewWillDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"CoserViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatController];
    [self getData];
    // Do any additional setup after loading the view.
}

-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight - 49 - 64) style:UITableViewStylePlain];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    _mainTable.rowHeight = 200;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTable];
}
-(void)getData {
    [self showIndeterminateProgress];
    _coser = [[NSMutableArray alloc]init];
    _cover = [[NSMutableArray alloc]init];
    _imgCount = [[NSMutableArray alloc]init];
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"coser"];
//    query.cachePolicy = kPFCachePolicyCacheElseNetwork;
//    //设置缓存有效期
//    query.maxCacheAge = 24 * 3600 * 7;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i < objects.count; i++) {
            [_imgCount addObject:[[objects objectAtIndex:i] objectForKey:@"imgCount"]];
            [_coser addObject:[[objects objectAtIndex:i] objectForKey:@"coserName"]];
            AVFile *file = [[objects objectAtIndex:i] objectForKey:@"coverImg"];
            [_cover addObject:file.url];
        }
        [_mainTable reloadData];
        [HUD hide:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_coser.count % 2 == 0) {
        return _cover.count / 2;
    } else {
        return _coser.count / 2 + 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.backgroundColor = [UIColor colorWithRed:1 green:0.98 blue:0.94 alpha:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(_cover.count % 2 == 0) {
        UIImageView *back1_2 = [[UIImageView alloc]initWithFrame:CGRectMake(27, 7, 112, 166)];
        back1_2.image = [UIImage imageNamed:@"IMG_0024_03"];
        back1_2.transform = CGAffineTransformMakeRotation(-0.08f);
        [cell.contentView addSubview:back1_2];
        
        UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
        image1.transform = CGAffineTransformMakeRotation(-0.08f);
        [image1 setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
        [cell.contentView addSubview:image1];
        
        UIImageView *back1 = [[UIImageView alloc]initWithFrame:CGRectMake(27, 7, 112, 166)];
        back1.image = [UIImage imageNamed:@"IMG_0024_03"];
        [cell.contentView addSubview:back1];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
        image.tag = indexPath.row * 2;
        [image addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [image setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
        [cell.contentView addSubview:image];
        
        UIImageView *number = [[UIImageView alloc]initWithFrame:CGRectMake(30, 142, 29, 29)];
        number.image = [UIImage imageNamed:@"58_02"];
        [cell.contentView addSubview:number];
        
        UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(35, 149, 100, 20)];
        count.textAlignment = NSTextAlignmentLeft;
        count.text = [NSString stringWithFormat:@"%@",[_imgCount objectAtIndex:indexPath.row * 2]];
        count.textColor = [UIColor whiteColor];
        count.font = [UIFont fontWithName:@"STXIHEI" size:12];
        [cell.contentView addSubview:count];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 175, 160, 20)];
        label.text = [_coser objectAtIndex:indexPath.row * 2];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
        
        UIImageView *back2_1 = [[UIImageView alloc]initWithFrame:CGRectMake(189, 9, 108, 162)];
        back2_1.image = [UIImage imageNamed:@"IMG_0024_03"];
        back2_1.transform = CGAffineTransformMakeRotation(-0.08f);
        [cell.contentView addSubview:back2_1];
        
        UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(190, 10, 106, 160)];
        image2.transform = CGAffineTransformMakeRotation(-0.08f);
        [image2 setImageURLStr:[_cover objectAtIndex:indexPath.row * 2 + 1] placeholder:[UIImage imageNamed:@"zhan"]];
        [cell.contentView addSubview:image2];
        
        UIImageView *back2_2 = [[UIImageView alloc]initWithFrame:CGRectMake(189, 9, 108, 162)];
        back2_2.image = [UIImage imageNamed:@"IMG_0024_03"];
        [cell.contentView addSubview:back2_2];
        
        UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(190, 10, 106, 160)];
        back.tag = indexPath.row * 2 + 1;
        back.userInteractionEnabled = YES;
        [back addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [back setImageURLStr:[_cover objectAtIndex:indexPath.row * 2 + 1] placeholder:[UIImage imageNamed:@"zhan"]];
        [cell.contentView addSubview:back];
        
        UIImageView *number2 = [[UIImageView alloc]initWithFrame:CGRectMake(190, 142, 29, 29)];
        number2.image = [UIImage imageNamed:@"58_02"];
        [cell.contentView addSubview:number2];
        
        UILabel *count2 = [[UILabel alloc]initWithFrame:CGRectMake(195, 149, 100, 20)];
        count2.textAlignment = NSTextAlignmentLeft;
        count2.textColor = [UIColor whiteColor];
        count2.font = [UIFont fontWithName:@"STXIHEI" size:12];
        count2.text = [NSString stringWithFormat:@"%@",[_imgCount objectAtIndex:indexPath.row * 2 + 1]];
        [cell.contentView addSubview:count2];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(160, 175, 160, 20)];
        label2.text = [_coser objectAtIndex:indexPath.row * 2 + 1];
        label2.font = [UIFont systemFontOfSize:15];
        label2.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label2];
    } else {
        if (indexPath.row == _cover.count / 2) {
            UIImageView *back1_2 = [[UIImageView alloc]initWithFrame:CGRectMake(29, 9, 108, 162)];
            back1_2.image = [UIImage imageNamed:@"IMG_0024_03"];
            back1_2.transform = CGAffineTransformMakeRotation(-0.08f);
            [cell.contentView addSubview:back1_2];
            
            UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
            image1.transform = CGAffineTransformMakeRotation(-0.08f);
            [image1 setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:image1];
            
            UIImageView *back1 = [[UIImageView alloc]initWithFrame:CGRectMake(29, 9, 108, 162)];
            back1.image = [UIImage imageNamed:@"IMG_0024_03"];
            [cell.contentView addSubview:back1];
            
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
            image.tag = indexPath.row * 2;
            image.userInteractionEnabled = YES;
            [image addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
            [image setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:image];
            
            UIImageView *number = [[UIImageView alloc]initWithFrame:CGRectMake(30, 142, 29, 29)];
            number.image = [UIImage imageNamed:@"58_02"];
            [cell.contentView addSubview:number];
            
            UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(35, 149, 100, 20)];
            count.textAlignment = NSTextAlignmentLeft;
            count.text = [NSString stringWithFormat:@"%@",[_imgCount objectAtIndex:indexPath.row * 2]];
            count.textColor = [UIColor whiteColor];
            count.font = [UIFont fontWithName:@"STXIHEI" size:12];
            [cell.contentView addSubview:count];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 175, 160, 20)];
            label.text = [_coser objectAtIndex:indexPath.row * 2];
            label.font = [UIFont systemFontOfSize:15];
            label.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:label];
        } else {
            UIImageView *back1_2 = [[UIImageView alloc]initWithFrame:CGRectMake(29, 9, 108, 162)];
            back1_2.image = [UIImage imageNamed:@"IMG_0024_03"];
            back1_2.transform = CGAffineTransformMakeRotation(-0.08f);
            [cell.contentView addSubview:back1_2];
            
            UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
            image1.transform = CGAffineTransformMakeRotation(-0.08f);
            [image1 setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:image1];
            
            UIImageView *back1 = [[UIImageView alloc]initWithFrame:CGRectMake(29, 9, 108, 162)];
            back1.image = [UIImage imageNamed:@"IMG_0024_03"];
            [cell.contentView addSubview:back1];
            
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 106, 160)];
            image.tag = indexPath.row * 2;
            image.userInteractionEnabled = YES;
            [image addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
            [image setImageURLStr:[_cover objectAtIndex:indexPath.row * 2] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:image];
            
            UIImageView *number = [[UIImageView alloc]initWithFrame:CGRectMake(30, 142, 29, 29)];
            number.image = [UIImage imageNamed:@"58_02"];
            [cell.contentView addSubview:number];
            
            UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(35, 149, 100, 20)];
            count.textAlignment = NSTextAlignmentLeft;
            count.text = [NSString stringWithFormat:@"%@",[_imgCount objectAtIndex:indexPath.row * 2]];
            count.textColor = [UIColor whiteColor];
            count.font = [UIFont fontWithName:@"STXIHEI" size:12];
            [cell.contentView addSubview:count];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 175, 160, 20)];
            label.text = [_coser objectAtIndex:indexPath.row * 2];
            label.font = [UIFont systemFontOfSize:15];
            label.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:label];
            
            UIImageView *back2_1 = [[UIImageView alloc]initWithFrame:CGRectMake(189, 9, 108, 162)];
            back2_1.image = [UIImage imageNamed:@"IMG_0024_03"];
            back2_1.transform = CGAffineTransformMakeRotation(-0.08f);
            [cell.contentView addSubview:back2_1];
            
            UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(190, 10, 106, 160)];
            image2.transform = CGAffineTransformMakeRotation(-0.08f);
            [image2 setImageURLStr:[_cover objectAtIndex:indexPath.row * 2 + 1] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:image2];
            
            UIImageView *back2_2 = [[UIImageView alloc]initWithFrame:CGRectMake(189, 9, 108, 162)];
            back2_2.image = [UIImage imageNamed:@"IMG_0024_03"];
            [cell.contentView addSubview:back2_2];
            
            UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(190, 10, 106, 160)];
            back.tag = indexPath.row * 2 + 1;
            back.userInteractionEnabled = YES;
            [back addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
            [back setImageURLStr:[_cover objectAtIndex:indexPath.row * 2 + 1] placeholder:[UIImage imageNamed:@"zhan"]];
            [cell.contentView addSubview:back];
            
            UIImageView *number2 = [[UIImageView alloc]initWithFrame:CGRectMake(190, 142, 29, 29)];
            number2.image = [UIImage imageNamed:@"58_02"];
            [cell.contentView addSubview:number2];
            
            UILabel *count2 = [[UILabel alloc]initWithFrame:CGRectMake(195, 149, 100, 20)];
            count2.textAlignment = NSTextAlignmentLeft;
            count2.textColor = [UIColor whiteColor];
            count2.font = [UIFont fontWithName:@"STXIHEI" size:12];
            count2.text = [NSString stringWithFormat:@"%@",[_imgCount objectAtIndex:indexPath.row * 2 + 1]];
            [cell.contentView addSubview:count2];
            
            UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(160, 175, 160, 20)];
            label2.text = [_coser objectAtIndex:indexPath.row * 2 + 1];
            label2.font = [UIFont systemFontOfSize:15];
            label2.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:label2];
        }
    }
    return cell;
}

-(void)buttonClick:(UITapGestureRecognizer *)sender {
    [MobClick event:@"goToCoser"];

    PicViewController *pic = [[PicViewController alloc]init];
    pic.coserName = [_coser objectAtIndex:sender.view.tag];
    pic.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pic animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
